/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pfc.modelocg;

import java.io.File;
import java.io.FileWriter;
import java.net.MalformedURLException;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.beanutils.BeanUtils;


import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import pfc.modelocg.CGForm.FormType;

/**
 *
 * @author xandru
 */
public class CGModelIO {

    public static String TAG_CGMODEL = "CGModel";
    public static String TAG_CFORM = "CGForm";
    public static String TAG_CGFIELD = "CGField";
    public static String TAG_SECURITY = "CGSecurity";
    public static String TAG_ENTITY_FORMS = "CGEntityForms";
    //private static String[] CGFormProperties = {};
    private static String[] CGFieldProperties = {};

    public static void model2XML(CGModel m, String fichero) {
        Document doc = null;
        try {

            doc = DocumentHelper.createDocument();

            Element root = doc.addElement(TAG_CGMODEL);

            Element sec = root.addElement(TAG_SECURITY);
            Element eF = root.addElement(TAG_ENTITY_FORMS);
            for (CGEntityForms e : m.getEntityForms()) {
                Element entityForm = eF.addElement("EntityForms");
                entityForm.addAttribute("entityName", e.getEntityName());
                entityForm.addAttribute("generateEditForm", e.isGenerateEditForm().toString());
                entityForm.addAttribute("generateLinesForm", e.isGenerateLinesForm().toString());
                entityForm.addAttribute("generateSearchForm", e.isGenerateSearchForm().toString());
                entityForm.addAttribute("generateLinkedForms", e.isGenerateLinkedForms().toString());
            }



            for (CGForm form : m.getForms()) {
                Element form1 = root.addElement(TAG_CFORM);

                form1.addAttribute("name", form.getName());
                form1.addAttribute("entity", form.getEntity().getName());
                form1.addAttribute("type", form.getType().toString());
                form1.addAttribute("numberOfColumns", form.getNumberOfColumns().toString());
                form1.addAttribute("numberOfLines", form.getNumberOfLines().toString());

                Element fields = form1.addElement("fields");
                for (CGField field : form.getFields()) {
                    Element field1 = fields.addElement(TAG_CGFIELD);

                    field1.addAttribute("name", field.getName());
                    field1.addAttribute("attribute", field.getAttrib().getName());
                    field1.addAttribute("attributeName", field.getAttrib().getName());

                    field1.addAttribute("visible", field.isVisible().toString());
                    field1.addAttribute("editable", field.isEditable().toString());
                    field1.addAttribute("createSelect", field.createSelect().toString());
                }
            }

            OutputFormat format = OutputFormat.createPrettyPrint();
            XMLWriter writer = new XMLWriter(new FileWriter(fichero), format);
            writer.write(doc);
            writer.flush();

        } catch (Exception ex) {
            Logger.getLogger(CGModelIO.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public static CGModel XML2Model(CGModel cg, String fichero) throws DocumentException, MalformedURLException {
        CGModelIO m = new CGModelIO();

        return m.doXML2Model(cg, fichero);
    }

    CGModel doXML2Model(CGModel cg, String fichero) throws DocumentException, MalformedURLException {

        SAXReader reader = new SAXReader();
        Document document = reader.read(new File(fichero));
        Element root = document.getRootElement();
        for (Iterator i = root.elementIterator(); i.hasNext();) {
            Element ele = (Element) i.next();
            //System.out.println(ele.getName());
            if (ele.getName().equals(TAG_SECURITY)) {
                continue;
            }

            if (ele.getName().equals(TAG_ENTITY_FORMS)) {
                for (Iterator j = ele.elementIterator(); j.hasNext();) {
                    Element tmp = (Element) j.next();
                    CGEntityForms efs = new CGEntityForms(cg,tmp.attributeValue("entityName"));
                    efs.setGenerateEditForm(Boolean.valueOf(tmp.attributeValue("generateEditForm")));
                    efs.setGenerateLinesForm(Boolean.valueOf(tmp.attributeValue("generateLinesForm")));
                    efs.setGenerateLinkedForms(Boolean.valueOf(tmp.attributeValue("generateLinkedForms")));
                    efs.setGenerateSearchForm(Boolean.valueOf(tmp.attributeValue("generateSearchForm")));
                    cg.addEntityForms(efs);
                }
                continue;
            }

            if (ele.getName().equals(TAG_CFORM)) {

                //System.out.println(ele.getPath());
                Element tmp = ele.element("fields");
                CGForm cgForm = CGForm.createCGForm();
                cgForm.setName(ele.attributeValue("name"));
                cgForm.setType(FormType.valueOf(ele.attributeValue("type")));
                cgForm.setEntity(null);
                cgForm.setEntityName(ele.attributeValue("entity"));
                cgForm.setNumberOfColumns(new Integer(ele.attributeValue("numberOfColumns")));
                cgForm.setNumberOfLines(new Integer(ele.attributeValue("numberOfLines")));

                for (Iterator j = tmp.elementIterator(); j.hasNext();) {
                    Element eleField = (Element) j.next();
                    //System.out.println(eleField.getPath());
                    CGField cgField = CGField.createField();
                    cgField.setName(eleField.attributeValue("name"));
                    cgField.setAttrib(null);
                    cgField.setAttribName(eleField.attributeValue("attributeName"));
                    cgField.setVisible(Boolean.valueOf(eleField.attributeValue("visible")));
                    cgField.setEditable(Boolean.valueOf(eleField.attributeValue("editable")));
                    cgField.setCreateSelect(Boolean.valueOf(eleField.attributeValue("createSelect")));
                    cgForm.addField(cgField);
                }
                // do something
                cg.addForm(cgForm);
            }
        }


        return cg;
    }
}
